package de.voegel;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

import de.voegel.controller.MainControl;

public class Startup {
	private static MainControl MAIN_CONTROL;
	private final static Logger logger = Logger.getRootLogger();

	public static void main(String[] args) {
		// Set cross-platform Java L&F (also called "Metal")
		try {
			SimpleDateFormat df = new SimpleDateFormat("E-dd-MM-yyyy",
					Locale.GERMANY);
			PatternLayout layout = new PatternLayout(
					"%-5p[%C{1}][%d{ISO8601}]: %m%n");
			File file = new File("logs/");
			if (!file.isDirectory()) {
				file.mkdirs();
			}
			FileAppender fileAppender = new FileAppender(layout, "logs/"
					+ df.format(new Date()) + ".log", true);
			logger.addAppender(fileAppender);
			logger.setLevel(Level.INFO);
		} catch (Exception ex) {
			System.out.println(ex);
			Logger.getRootLogger().error(ex.getMessage());
		}
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("LookAndFell konnte nicht gesetzt werden -> "
					+ e.getMessage());
		}
		try {
			MAIN_CONTROL = new MainControl(logger);
		} catch (Exception e) {
			JOptionPane.showConfirmDialog(null,
					String.format("Fehler: %s", e.getMessage()));
			logger.error(String.format("Fehler: %s", e.getMessage()));
		}
	}

}
